
cc.Class({
    extends: cc.Component,

    properties: {
        isRunning: false,
        reelId: 0,
    },

    onLoad() {
        this.wlddMain = cc.find("Canvas/game/game_wldd").getComponent("wlddMain");
        this.wlddMain.reels[this.reelId] = this;
        this.lastSymbol = [];
        this.lastSymbol = this.initLastSymbol();
        this.lastSymbol2 = this.lastSymbol;
        this.speed = 3000;
        this.isStop = false;
        this.symbolsNum = 40;
        this.reelTime = 3;
        this.symbolPrefabs = [];
    },

    initReel() {
        this.state = 0;  //0是空闲状态  1是结果到了 2是结束状态 只有第一条是2 其他的才可以结束
        this.node.removeAllChildren();
        this.addLastSymbol(true);
        this.addNPCprefab();
        this.node.y = -240
    },

    addNPCprefab() {
        this.addLastSymbol();
        for (let i = 0; i < this.symbolsNum - 3; i++) {
            let random = Math.floor(Math.random() * 9);
            this.addPrefab(random);
        }
    },
    startReel() {
        this.node.removeAllChildren();
        this.addLastSymbol(false);
        this.addNPCprefab();
        let startY = -240;
        this.node.y = startY;
        // 锁定动画开始时的状态
        let currentState = this.state;

        let targetY;
        if (currentState === 1) {
            let data = this.lastSymbol2;
            if (data[0] == data[1] && data[1] == data[2]) {
                this.addPrefab(11, true);
            } else {
                for (let i = data.length - 1; i >= 0; i--) {
                    this.addPrefab(data[i], true);
                }
            }
            targetY = startY - (160 * (this.symbolsNum + 3));
        } else {
            targetY = startY - (160 * (this.symbolsNum));
        }

        this.symbolPrefabs = this.node.children;
        let self = this;

        cc.tween(this.node)
            .to(this.reelTime, { y: targetY }, { easing: 'linear' })
            .call(() => {
                if (currentState === 1) {  // 用锁定的状态判断
                    if (self.reelId === 0 || self.wlddMain.reels[0].state === 2) {
                        self.wlddMain.afterReel(self.reelId);
                        self.state = 2;
                    } else {
                        self.startReel();
                    }
                } else {
                    self.startReel();
                }
            })
            .start();
    },

    addPrefab(random, bool) {  //bool为真的是服务器传回的
        let symbol = cc.instantiate(this.wlddMain.iconPrefab[random]);
        if (!bool) {
            symbol.getChildByName("symbol").active = false;
            symbol.getChildByName("blur").active = true;
        }
        this.node.addChild(symbol);
    },


    //添加服务器返回的数据
    addResponse(data) {
        this.lastSymbol2 = data;
        // this.node.removeAllChildren();
        // this.addLastSymbol();
        // this.addNPCprefab();
        // if (data[0][0] == data[1][0] && data[1][0] == data[2][0]) {
        //     this.addPrefab(11);
        // } else {
        //     for (let i = data.length - 1; i >= 0; i--) {
        //         this.addPrefab(data[i]);
        //     }
        // }
    },

    addLastSymbol(bool) {
        if (this.lastSymbol[0] == this.lastSymbol[1] && this.lastSymbol[1] == this.lastSymbol[2] && this.lastSymbol[0]) {
            this.addPrefab(11, bool);
        } else {
            for (let i = this.lastSymbol.length - 1; i >= 0; i--) {
                this.addPrefab(this.lastSymbol[i], bool);
            }
        }
        this.lastSymbol = this.lastSymbol2
    },

    // 初始化最后3个symbol
    initLastSymbol() {
        let arr = [];
        for (let i = 0; i < 3; i++) {
            let random = Math.floor(Math.random() * 10);
            arr.push(random);
        }
        return arr;
    },


    update(dt) {
    }
});
